package com.ft.adbix.core.ad;

import android.content.Context;

import com.ft.adbix.core.ADBixSdk;
import com.ft.adbix.core.base.AdParams;
import com.ft.adbix.core.common.ErrorCode;
import com.ft.adbix.core.response.Ad;
import com.ft.adbix.utils.AdLogUtils;
import com.ft.adbix.utils.http.HttpHelper;
import com.ft.adbix.utils.http.listener.CommonAdListener;
import com.ft.adbix.utils.http.request.RequestUtil;

import java.util.Map;

public class AdBixAdLoader implements AdBixAdNative{

    private Context context;
    public AdBixAdLoader(Context context){
        this.context = context;
    }

    @Override
    public void loadNativeAd(AdParams adParams, NativeAdListener listener) {
        if (!ADBixSdk.getInstance().isInit()){
            if (listener!=null){
                listener.onError(ErrorCode.ERROR_NOT_INIT,"SDK未初始化");
            }
            return;
        }
        Map<String, Object> adMap = RequestUtil.transAdParams(adParams);
        String s = HttpHelper.gson.toJson(adMap);
        AdLogUtils.d("request Ad = "+s);
        HttpHelper.loadAd(adMap, new CommonAdListener() {
            @Override
            public void onResponse(Ad result) {
                NativeAd nativeAd = new NativeAd(context, result);
                if (listener != null) {
                    listener.onNativeAdLoad(nativeAd);
                }
//                nativeAd.renderAd(context);
            }

            @Override
            public void onError(int code, String error) {
                if (listener != null) {
                    listener.onError(code, error);
                }
            }
        });
    }

    @Override
    public void loadSplashAd(AdParams adParams, SplashAdListener listener) {
        if (!ADBixSdk.getInstance().isInit()){
            if (listener!=null){
                listener.onError(ErrorCode.ERROR_NOT_INIT,"SDK未初始化");
            }
            return;
        }
        Map<String, Object> adMap = RequestUtil.transAdParams(adParams);
        HttpHelper.loadAd(adMap, new CommonAdListener() {
            @Override
            public void onResponse(Ad result) {
                AdLogUtils.d("splash_onResponse = "+result.toString());
                result.setReqId(adParams.getReqId());
                SplashAd splashAd = new SplashAd(context, result);
                if (listener != null) {
                    listener.onSplashLoadSuccess(splashAd);
                }
            }

            @Override
            public void onError(int code, String error) {
                if (listener != null) {
                    listener.onError(code, error);
                }
            }
        });
    }

    @Override
    public void loadRewardVideoAd(AdParams adParams, RewardVideoAdListener listener) {
        if (!ADBixSdk.getInstance().isInit()){
            if (listener!=null){
                listener.onError(ErrorCode.ERROR_NOT_INIT,"SDK未初始化");
            }
            return;
        }
        Map<String, Object> adMap = RequestUtil.transAdParams(adParams);
        HttpHelper.loadAd(adMap, new CommonAdListener() {
            @Override
            public void onResponse(Ad result) {
                AdLogUtils.d("onResponse = "+result.toString());
                result.setReqId(adParams.getReqId());
                RewardAd rewardAd = new RewardAd(context, result);
                if (listener != null) {
                    listener.onRewardVideoAdLoad(rewardAd);
                }
            }

            @Override
            public void onError(int code, String error) {
                if (listener != null) {
                    listener.onError(code, error);
                }
            }
        });
    }

    @Override
    public void loadInterstitialAd(AdParams adParams, InterstitialAdListener listener) {
        if (!ADBixSdk.getInstance().isInit()){
            if (listener!=null){
                listener.onError(ErrorCode.ERROR_NOT_INIT,"SDK未初始化");
            }
            return;
        }
        Map<String, Object> adMap = RequestUtil.transAdParams(adParams);
        String s = HttpHelper.gson.toJson(adMap);
        AdLogUtils.d("request Ad = "+s);
        HttpHelper.loadAd(adMap, new CommonAdListener() {
            @Override
            public void onResponse(Ad result) {
                result.setReqId(adParams.getReqId());
                InterstitialAd interstitialAd = new InterstitialAd(context, result);
                if (listener != null) {
                    listener.onInterstitialAdLoad(interstitialAd);
                }
//                interstitialAd.renderAd(context);
            }

            @Override
            public void onError(int code, String error) {
                AdLogUtils.d("Interstitial ad load error! code="+code+", msg = "+error);
                if (listener != null) {
                    listener.onError(code, error);
                }
            }
        });
    }


    @Override
    public void loadNativeExpressAd(AdParams adParams, NativeExpressAdListener listener) {
        if (!ADBixSdk.getInstance().isInit()){
            if (listener!=null){
                listener.onError(ErrorCode.ERROR_NOT_INIT,"SDK未初始化");
            }
            return;
        }

    }

    @Override
    public void loadBannerAd(AdParams adParams, BannerAdListener listener) {
        if (!ADBixSdk.getInstance().isInit()){
            if (listener!=null){
                listener.onError(ErrorCode.ERROR_NOT_INIT,"SDK未初始化");
            }
            return;
        }
        Map<String, Object> adMap = RequestUtil.transAdParams(adParams);
        String s = HttpHelper.gson.toJson(adMap);
        AdLogUtils.d("request Ad = "+s);
        HttpHelper.loadAd(adMap, new CommonAdListener() {
            @Override
            public void onResponse(Ad result) {
                BannerAd bannerAd = new BannerAd(context, result);
                if (listener != null) {
                    listener.onBannerAdLoad(bannerAd);
                }
//                interstitialAd.renderAd(context);
            }

            @Override
            public void onError(int code, String error) {
                if (listener != null) {
                    listener.onError(code, error);
                }
            }
        });
    }

    @Override
    public void loadInteractionExpressAd(AdParams var1, NativeExpressAdListener var2) {

    }
}
